package flc.ast.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.os.Bundle;
import android.view.View;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.PathUtils;
import com.blankj.utilcode.util.ToastUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.HomeRecordAdapter;
import flc.ast.adapter.RecordAdapter;
import flc.ast.bean.MyVideoBean;
import flc.ast.databinding.ActivityRecordBinding;
import flc.ast.utils.Content;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.utils.MediaUtil;
import stark.common.basic.utils.RxUtil;

public class RecordActivity extends BaseAc<ActivityRecordBinding> {

    private List<MyVideoBean> listShow = new ArrayList<>();
    private RecordAdapter recordAdapter;

    @Override
    protected int onCreate() {
        return R.layout.activity_record;
    }

    @Override
    protected void initView() {
        mDataBinding.ivRecordBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.ivRecordDelete.setOnClickListener(this);

        mDataBinding.rvRecordList.setLayoutManager(new GridLayoutManager(mContext, 2));
        recordAdapter = new RecordAdapter();
        mDataBinding.rvRecordList.setAdapter(recordAdapter);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivRecordDelete://删除
                deleteFile();
                break;
        }
    }

    @Override
    protected void initData() {
        getData();
    }

    private void getData() {
        listShow.clear();
        //获取文件夹下文件
        String path = PathUtils.getFilesPathExternalFirst() + Content.myRecord;
        List<File> files = FileUtils.listFilesInDir(path);
        if (files.size() > 0) {
            for (File file : files) {
                listShow.add(new MyVideoBean(file.getPath(), file.getName(), MediaUtil.getDuration(file.getPath()), FileUtils.getFileLastModified(file.getPath())));
            }
        }
        if (listShow.size() > 0) {
            recordAdapter.setList(listShow);
            mDataBinding.rvRecordList.setVisibility(View.VISIBLE);
            mDataBinding.llRecordNoData.setVisibility(View.GONE);
        } else {
            mDataBinding.rvRecordList.setVisibility(View.GONE);
            mDataBinding.llRecordNoData.setVisibility(View.VISIBLE);
        }
    }

    //删除
    private void deleteFile() {
        RxUtil.create(new RxUtil.Callback<String>() {

            @Override
            public void doBackground(ObservableEmitter<String> emitter) {
                for (MyVideoBean bean : listShow) {
                    FileUtils.delete(bean.getPath());
                }
                emitter.onNext("");
            }

            @Override
            public void accept(String path) {
                ToastUtils.showShort(getString(R.string.delete_suc));
                getData();
            }
        });
    }

}